package com.admission.web.admin;

import java.io.File;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.model.ProvinceForm;
import com.admission.service.CollegeProvinceManager;
import com.admission.service.ProvinceFormManager;
import com.admission.util.NewFileName;
import com.admission.util.PageUtils;
import com.admission.web.CrudActionSupport;
@Namespace("/admin")
public class ProvinceFormAdminAction extends CrudActionSupport<ProvinceForm>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private CollegeProvinceManager collegeProvinceManager;
	private String id;
	private String ids;
	private ProvinceForm entity;
	private Page<ProvinceForm> page = new Page<ProvinceForm>(PageUtils.DEFAULT_PAGE_SIZE);
	private File collegeFile;
	private String collegeFileFileName;
	private ProvinceFormManager provinceFormManager;
	
	public ProvinceForm getModel() {
		return entity;
	}

	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		page.setOrder(Page.DESC);
		page.setOrderBy("inputTime");
		page = provinceFormManager.getPage(page, filters);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		String uploadPath = ServletActionContext.getServletContext()
				.getRealPath("/upload"); 
		File dir = new File(uploadPath);
		if (!dir.exists()) {
			dir.mkdir();
		}
		String subPath = null;
		try{
		if (collegeFile != null) {
			subPath = new String();
				subPath = NewFileName.genNewFileName(
						collegeFileFileName, 0);
				FileUtils.copyFile(collegeFile, new File(uploadPath,
						subPath));
				entity.setTitle(collegeFileFileName.substring(0, collegeFileFileName.lastIndexOf(".")));
				provinceFormManager.save(entity,subPath);
				collegeProvinceManager.save(uploadPath + File.separator
						+ subPath,entity.getId());
			}
		}
		catch (Exception e) {
				provinceFormManager.deleteByIds(entity.getId());
			   this.addActionMessage("请按照省份公告模板表进行编辑，详细错误信息可在日志管理处查看，如果没有权限可联系超级管理员");
			   return INPUT;
		    }
		return list();
	}

	@Override
	public String delete() throws Exception {
		provinceFormManager.deleteByIds(ids);
		return list();
	}

	@Override
	protected void prepareModel() throws Exception {
		if(StringUtils.isEmpty(id))
			entity=new ProvinceForm();
		else
			entity=provinceFormManager.getById(id);
		
	}

	public CollegeProvinceManager getCollegeProvinceManager() {
		return collegeProvinceManager;
	}
	@Autowired
	public void setCollegeProvinceManager(
			CollegeProvinceManager collegeProvinceManager) {
		this.collegeProvinceManager = collegeProvinceManager;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public ProvinceForm getEntity() {
		return entity;
	}

	public void setEntity(ProvinceForm entity) {
		this.entity = entity;
	}

	public Page<ProvinceForm> getPage() {
		return page;
	}

	public void setPage(Page<ProvinceForm> page) {
		this.page = page;
	}

	public File getCollegeFile() {
		return collegeFile;
	}

	public void setCollegeFile(File collegeFile) {
		this.collegeFile = collegeFile;
	}

	public String getCollegeFileFileName() {
		return collegeFileFileName;
	}

	public void setCollegeFileFileName(String collegeFileFileName) {
		this.collegeFileFileName = collegeFileFileName;
	}

	public ProvinceFormManager getProvinceFormManager() {
		return provinceFormManager;
	}
	@Autowired
	public void setProvinceFormManager(ProvinceFormManager provinceFormManager) {
		this.provinceFormManager = provinceFormManager;
	}



}
